//
//  LLRemarkViewController.swift
//  LiLi
//
//  Created by liuzhu on 2019/1/24.
//  Copyright © 2019 liuzhu. All rights reserved.
//

import UIKit

let LLRemarkListIdentifier = "LLRemarkListIdentifier"
class LLRemarkViewController: UIViewController {

    public var action:LLAction?{
        didSet{
//            let remarks = DataManager.shareManger().QueryRemarks(actionId: (action?.recordName)!)
//            self.remarks = remarks
        }
    }
    
    public var remarks:[LLRemark]?{
        didSet{
            self.tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "write", target: self, action: #selector(rightItemClick))
        self.title = "日记"
        self.view.addSubview(tableView)
        tableView.register(LLRemarkCell.self, forCellReuseIdentifier: LLRemarkListIdentifier)//注册cell
    }

    private lazy var tableView:UITableView = {
        var y = CGFloat(64)
        if LLConst.getPhoneType() == PhoneType.PHONE_MAX_XR || LLConst.getPhoneType() == PhoneType.PHONE_X {
            y = 88
        }
        let t = UITableView.init(frame:CGRect(x: 0, y: y, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - y))
        t.estimatedRowHeight = 210
        t.rowHeight = UITableView.automaticDimension
        t.delegate = self
        t.dataSource = self
        t.separatorStyle = .none
        return t
    }()
    
    @objc func rightItemClick(){
        let vc = LLCreateRemarkViewController()
        vc.action = action!
       self.navigationController?.pushViewController(vc, animated: false)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        self.tabBarController?.tabBar.isHidden = true
        let remarks = DataManager.QueryRemarks(actionId: (action?.recordName)!)
        self.remarks = remarks
    }
    
    @objc private func deleteRemark(btn:UIButton){
        let alertVC = UIAlertController(title: "提示", message: "你确定要删除该日记么", preferredStyle: UIAlertController.Style.alert)
        let actionSure = UIAlertAction(title: "确定", style: UIAlertAction.Style.default) { (_) in
            let remark = self.remarks![btn.tag]
            DataManager.deleteRemark(recordName: remark.recordName!) { (isSuccess) in
                if isSuccess{
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: LLRemarkDeleteKey), object: nil, userInfo: ["remark":remark])
                    self.remarks = DataManager.QueryRemarks(actionId: (self.action?.recordName)!)
                    DispatchQueue.main.async {
                          self.tableView.reloadData()
                    }
                }
            }
        }
        let actionCancel = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel) { (_) in
            
        }
        alertVC.addAction(actionSure)
        alertVC.addAction(actionCancel)
        self.present(alertVC, animated: true, completion: nil)
    }
}

extension LLRemarkViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let count = remarks?.count ?? 0
        return count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: LLRemarkListIdentifier, for: indexPath) as! LLRemarkCell
        let remark = remarks![indexPath.row]
        cell.remark = remark
        cell.deleteBtn.tag = indexPath.row
        cell.deleteBtn.addTarget(self, action: #selector(deleteRemark(btn:)), for: UIControl.Event.touchUpInside)
        cell.selectionStyle = UITableViewCell.SelectionStyle.none;
        return cell
    }
}
